package com.gxa.uhome.controller;

import com.gxa.uhome.exception.BusinessException;
import com.gxa.uhome.pojo.UserAddress;
import com.gxa.uhome.service.UserAddressService;
import com.gxa.uhome.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * User: Sanruy Mu
 * Date: 2021/06/07 13:49
 * Description:
 * Version: V1.0
 */
@CrossOrigin
@RestController
@RequestMapping(value = "/userAddress", method = RequestMethod.POST)
@Api(tags = "UserAddress用户地址模块管理", produces = "application/json;charset=UTF-8")
public class UserAddressController {
    @Resource
    UserAddressService userAddressService;

    @ApiOperation(value = "用户地址添加")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "userAddress", value = "json格式的用户地址对象,包括userId,name,phone,buphone(非必须),address",
                    paramType = "body")
    )
    @PostMapping("/addUserAddress")
    public JsonResult addUserAddress(@RequestBody UserAddress userAddress) {
        JsonResult jsonResult = new JsonResult();
        try {
            Integer integer = userAddressService.addAddress(userAddress);
            if (integer != 0) {
                jsonResult.setCode("1");
                jsonResult.setMsg("添加成功");
            }
        } catch (BusinessException e) {
            jsonResult.setCode("0");
            jsonResult.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "用户收货信息修改")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "userAddress", value = "json格式的用户地址对象(id,userId,name,phone,buphone(非必须),address)",
                    paramType = "body")
    )
    @PostMapping("/changeUserAddress")
    public JsonResult changeUserAddress(@RequestBody UserAddress userAddress) {
        JsonResult jsonResult = new JsonResult();
        try {
            Integer edit = userAddressService.edit(userAddress);
            if (edit != 0) {
                jsonResult.setCode("1");
                jsonResult.setMsg("修改成功");
            }
        } catch (BusinessException e) {
            jsonResult.setCode("0");
            jsonResult.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return jsonResult;
    }


    @ApiOperation(value = "用户地址删除")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id", value = "json格式的地址id",
                    paramType = "query")
    )
    @PostMapping("/deleteUserAddress")
    public JsonResult deleteUserAddress(@RequestParam(value = "id") Integer id) {
        JsonResult jsonResult = new JsonResult();
        try {
            Integer integer = userAddressService.deleteAddress(id);
            if (integer != 0) {
                jsonResult.setCode("1");
                jsonResult.setMsg("删除成功");
            }
        } catch (BusinessException e) {
            jsonResult.setCode("0");
            jsonResult.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "用户地址查看")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id", value = "json格式的用户id",
                    paramType = "query")
    )
    @PostMapping("/select")
    public JsonResult select(@RequestParam(value = "id") Integer id) {
        JsonResult jsonResult = new JsonResult();
        try {
            List<UserAddress> integer = userAddressService.selectByUserId(id);
            if (integer != null) {
                jsonResult.setCode("1");
                jsonResult.setMsg("查询成功");
                jsonResult.setData(integer);
            }
        } catch (BusinessException e) {
            jsonResult.setCode("0");
            jsonResult.setMsg(e.getMessage());
        }
        return jsonResult;
    }

}